<template>
  <div class="publish-page">
    <h1 class="section-title">发布二手商品</h1>
    
    <div v-if="!user.isLoggedIn" class="not-logged-in">
      <h2>请先登录</h2>
      <p>登录后可以发布二手商品</p>
      <router-link to="/login" class="btn btn-primary">立即登录</router-link>
    </div>
    
    <div v-else class="publish-form">
      <form @submit.prevent="submitForm">
        <div class="form-group">
          <label class="form-label">商品标题</label>
          <input 
            type="text" 
            class="form-control" 
            placeholder="请输入商品标题" 
            v-model="form.title"
            required
          >
        </div>
        
        <div class="form-group">
          <label class="form-label">商品描述</label>
          <textarea 
            class="form-control" 
            placeholder="详细描述商品信息..." 
            rows="4"
            v-model="form.description"
            required
          ></textarea>
        </div>
        
        <div class="form-group">
          <label class="form-label">商品分类</label>
          <select class="form-control" v-model="form.category" required>
            <option value="">请选择分类</option>
            <option value="教材书籍">教材书籍</option>
            <option value="电子数码">电子数码</option>
            <option value="生活用品">生活用品</option>
            <option value="服装鞋帽">服装鞋帽</option>
            <option value="体育用品">体育用品</option>
            <option value="其他">其他</option>
          </select>
        </div>
        
        <div class="form-row">
          <div class="form-group">
            <label class="form-label">价格 (¥)</label>
            <input 
              type="number" 
              class="form-control" 
              placeholder="0.00" 
              min="0"
              step="0.01"
              v-model="form.price"
              required
            >
          </div>
          
          <div class="form-group">
            <label class="form-label">上传图片</label>
            <div class="image-upload">
              <label for="file-upload" class="upload-btn">
                {{ form.image ? '更换图片' : '选择图片' }}
              </label>
              <input 
                id="file-upload" 
                type="file" 
                accept="image/*" 
                @change="handleImageUpload"
                style="display: none;"
              >
              <div v-if="form.image" class="image-preview">
                <div class="preview-image" :class="`image-${form.image}`"></div>
              </div>
            </div>
          </div>
        </div>
        
        <div class="form-group">
          <label class="form-label">联系方式</label>
          <input 
            type="text" 
            class="form-control" 
            placeholder="手机号或微信号"
            v-model="form.contact"
            required
          >
        </div>
        
        <div class="form-actions">
          <button type="button" class="btn" @click="$router.push('/')">取消</button>
          <button type="submit" class="btn btn-primary">发布商品</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { ref, inject } from 'vue'
import { useRouter } from 'vue-router'

export default {
  setup() {
    const router = useRouter()
    const user = inject('user')
    const items = inject('items')
    const addItem = inject('addItem')
    
    const form = ref({
      title: '',
      description: '',
      category: '',
      price: '',
      image: '',
      contact: ''
    })
    
    const handleImageUpload = (event) => {
      const file = event.target.files[0]
      if (file) {
        const imageTypes = ['book', 'laptop', 'basketball', 'lamp']
        const randomImage = imageTypes[Math.floor(Math.random() * imageTypes.length)]
        form.value.image = randomImage
      }
    }
    
    const submitForm = () => {
      const newId = items.value.length > 0 
        ? Math.max(...items.value.map(item => item.id)) + 1 
        : 1
      
      const newItem = {
        id: newId,
        title: form.value.title,
        description: form.value.description,
        category: form.value.category,
        price: parseFloat(form.value.price),
        image: form.value.image,
        seller: user.name,
        contact: form.value.contact,
        publishDate: new Date().toISOString().split('T')[0]
      }
      
      addItem(newItem)
      user.itemsPosted += 1
      
       localStorage.setItem('user', JSON.stringify({
        name: user.name,
        college: user.college,
        itemsPosted: user.itemsPosted,
        itemsSold: user.itemsSold,
        rating: user.rating
      }))

      alert('商品发布成功！')
      router.push('/')
    }
    
    return {
      user,
      form,
      handleImageUpload,
      submitForm
    }
  }
}
</script>


<style scoped>
.not-logged-in {
  text-align: center;
  padding: 60px 20px;
  background: white;
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  max-width: 600px;
  margin: 40px auto;
}

.not-logged-in h2 {
  color: var(--primary-color);
  margin-bottom: 15px;
}

.not-logged-in p {
  color: var(--gray-color);
  margin-bottom: 25px;
  font-size: 1.1rem;
}

.publish-form {
  background: white;
  padding: 30px;
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  max-width: 800px;
  margin: 0 auto;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.image-upload {
  display: flex;
  align-items: center;
  gap: 15px;
}

.upload-btn {
  background: var(--light-color);
  border: 1px dashed #ccc;
  padding: 8px 15px;
  border-radius: var(--border-radius);
  cursor: pointer;
  transition: all 0.3s;
}

.upload-btn:hover {
  background: #e9ecef;
  border-color: var(--primary-color);
}

.image-preview {
  width: 80px;
  height: 80px;
  border-radius: var(--border-radius);
  overflow: hidden;
}

.preview-image {
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
}

.image-book {
  background-color: #e3f2fd;
}

.image-laptop {
  background-color: #e8f5e9;
}

.image-basketball {
  background-color: #fff3e0;
}

.image-lamp {
  background-color: #fff8e1;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  margin-top: 30px;
}

@media (max-width: 768px) {
  .form-row {
    grid-template-columns: 1fr;
  }
}
</style>